﻿using Es.Abp.Domain.Entities;
using Es.Abp.Domain.IRepository;
using Es.Abp.IApplication.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace Es.Abp.Application.Services
{
    public class StudentService : ApplicationService
    {
        private IStudentRepository _studentRepository;
        public StudentService(IStudentRepository studentRepository)
        {
            _studentRepository = studentRepository;
        }


        public async Task<List<StudentDto>> GetListAsync()
        {

            var entities = await _studentRepository.GetStudentAsync();

            var output = entities.Select(x => new StudentDto { Name = x.Name, Remarks = x.Remarks, Id = x.Id }).ToList();

            return output;
        }

        public void InitTable()
        {
            _studentRepository.InitTableAsync();

        }

        public async Task<StudentDto> Create(StudentDto dto)
        {
            if (dto.IsNameSuccess())
            {

                throw new UserFriendlyException("名称错误");
            }
            var entity = new StudentEntity();
            entity.Name = dto.Name;
            entity.Remarks = dto.Remarks;

           var r= await _studentRepository.CreateStudentAsync(entity);
            dto.Id=r.Id;
            return dto;
        }
    }
}
